package dljs.classes;

import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Ariman
 */
public class Deck {
    private ArrayList<CCard> cards;

    public Deck() {
        cards = new ArrayList<CCard>();
    }
    
    public void of52Cards(){
        fillDeck(2);
    }
    public void of36Cards(){
        fillDeck(6);
    }
    public void ofPrefCards(){
        fillDeck(7);
    }

    private void fillDeck(int firstNumber){
        clearDeck();
        //System.out.println("Begin creating deck");
        for(int num=firstNumber;num<15;num++){                
                cards.add(new CCard(num, ECardSuite.CLUBS));
                //System.out.println(num+" card 1");
                cards.add(new CCard(num, ECardSuite.DIAMONDS));
                //System.out.println(num+" card 2");
                cards.add(new CCard(num, ECardSuite.HEARTS));
                //System.out.println(num+" card 3");
                cards.add(new CCard(num, ECardSuite.SPADES));
                //System.out.println(num+" card 4");
        } 
        //System.out.println("End creating deck");
    }
    
    public void shuffle(){
        ArrayList<CCard> tempCards = (ArrayList<CCard>) cards.clone();
        //System.out.println("Before");
        //System.out.println("cards -  "+cards.toString()+"dlinna = "+cards.size());
        int num = cards.size();
        cards.clear();
        for(int i=0;i<num;i++){            
            int j=(int)Math.round(Math.random()*(num-1-i));
            cards.add(tempCards.get(j));
            tempCards.remove(j);            
        }
        //System.out.println("After");
        //System.out.println("cards -  "+cards.toString()+"dlinna = "+cards.size());
    }
    
    public CCard getTop(){
        if (cards.isEmpty()){
            return null;
        }
        else {
            CCard card =cards.get(cards.size()-1);
            cards.remove(cards.size()-1);
            return card;
        }        
    }
    public int size(){
        if (isEmpty()){
            return 0;
        }
        else {
            return cards.size();
        }
    }
    
    public void setSolitarePriority(){
        for (int i = 0; i < cards.size(); i++) {
            CCard cCard = cards.get(i);
            if(cCard.getRank()!=14){
                cCard.setPriority(cCard.getRank());
            }else{
                cCard.setPriority(1);
            }
        }
    }
    
    public boolean isEmpty(){        
        return cards.isEmpty();
    }
    
    public void clearDeck(){    
        cards.clear();
    }

    @Override
    public String toString() {
        return "Deck{" + "cards=" + cards + '}';
    }
    
}
